package com.fy.fyzf.weight;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.text.SpannableStringBuilder;
import android.text.style.ForegroundColorSpan;
import android.util.AttributeSet;
import android.widget.TextView;

import androidx.annotation.Nullable;

@SuppressLint("AppCompatCustomView")
public class MyTextView1 extends TextView {

    private SpannableStringBuilder spannableStringBuilder;

    public MyTextView1(Context context) {
        super(context);
        init();
    }

    public MyTextView1(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public MyTextView1(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        setText(spannableStringBuilder);
    }

    public void init() {
        spannableStringBuilder = new SpannableStringBuilder(getText());
        spannableStringBuilder.setSpan(new ForegroundColorSpan(Color.parseColor("#333333")), 0, getText().toString().length() - 2, spannableStringBuilder.SPAN_EXCLUSIVE_EXCLUSIVE);
        spannableStringBuilder.setSpan(new ForegroundColorSpan(Color.parseColor("#E92000")), getText().toString().length() - 2, getText().toString().length() -1, spannableStringBuilder.SPAN_EXCLUSIVE_EXCLUSIVE);
        spannableStringBuilder.setSpan(new ForegroundColorSpan(Color.parseColor("#333333")), getText().toString().length() - 1, getText().toString().length() , spannableStringBuilder.SPAN_EXCLUSIVE_EXCLUSIVE);

    }

}
